<?php
if (!isset($_SESSION)){
	session_start();
	if(!isset($_SESSION['username'])){
		$_SESSION['username']='';
	}
}
// if ($_SESSION['username']==NULL){
	// exit('<p>提问请先<a href="login.php?action=answer&q={$_GET['q']}">登录</a>!</p>');
// }

if (!empty($_POST))
{
	$username=$_SESSION['username'];
	$qid = trim($_GET['q']);
	$content = trim($_POST['contents']);
	$posttime=date("Y-m-d H:i:s",time());
	require_once('lib/connbae.php');	
	if(strlen($content)>9999)
		exit('字数超出限额！点击此处 <a href="javascript:history.back(-1);">返回</a>');

	$aid=mysql_result(mysql_query('select max(aid) FROM Answer'),0);$aid+=1;
	
	$sql = "INSERT INTO Answer(aid,qid,username,content,post_time) VALUES('$aid','$qid','$username','$content','$posttime');";
	echo $sql;
	$result =mysql_query($sql);
	if (!$result)
	{
		echo mysql_error().'<br/>';
		mysql_close();
		exit('数据记录插入失败！点击此处 <a href="javascript:history.back(-1);">返回</a>');
	}  
	mysql_close();
	exit('提交成功 <a href="index.php">返回主页</a>');	

}?>

<?php
if (!empty($_GET))
{

	$q = trim($_GET['q']);
	require_once('lib/connbae.php');
	$sql = "SELECT * FROM Question WHERE qid=".$q;	
	$result =mysql_query($sql);		
	if (!$result)
	{
		echo mysql_error().'<br/>';
		mysql_close();
		exit('数据查询失败！点击此处 <a href="javascript:history.back(-1);">返回</a>');
	}  
	mysql_close();
}
?>
<!DOCTYPE HTML>
<html>
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>C Programing Learning</title>
	<link rel="stylesheet" type="text/css" href="css/base.css"/>
	<link rel="stylesheet" type="text/css" href="css/ask.css"/>
	<link href="img/favour.ico" type="image/ico" rel="shortcut icon">
	<script type="text/javascript" src="#"></script>
<SCRIPT language="javascript"> 
<!-- 
function checktext(text) 
{ 
	allValid = true; 
	for (i = 0; i < text.length; i++) 
	{ 
		if (text.charAt(i) != " ")
		{ 
			allValid = false; 
			break; 
		} 
	} 
	return allValid; 
} 

function checkinput(message,total,used,remain) 
{
	var max; 
	max = total.value; 
	if (message.value.length > max) { 
		message.value = message.value.substring(0,max); 
		used.value = max; 
		remain.value = 0; 
	} 
	else { 
		used.value = message.value.length; 
		remain.value = max - used.value; 
	} 
} 
--> 
</script>
</head>
<body>
	<div id="content">
		<div id="container">
			<div id="header">
				<div class="nav">
					<ul>
						<li><a class="logo" href="/" title="Funny C"></a></li>
						<li><a href="/">主页</a></li>
						<li><a href="ask.php">提问</a></li>
						<li><a href="documation.php">资源</a></li>
<?php if(!isset($_SESSION['username'])||$_SESSION['username']=='')
					{
echo "						<li><a href='login.php?action=login'>登录</a></li>
						<li><a href='register.php'>注册</a></li>";
					}else{
echo "						<li><a href='my.php'>个人中心</a></li>
						<li><a href='login.php?action=logout'>注销</a></li>\n";
					} ?>
					</ul>
				</div>	
			</div><hr/>
			<div id="ask">
				<form action="answer.php?q=<?php echo $q;?>" method="post">
					<p>
						<label for="title" class="describle">问题描述:</label><br/>
						<textarea  readonly id="title" name="title" rows="2" cols="32"><?php 
						while($row = mysql_fetch_array($result)){echo $row['title']."\n".$row['content'];}?></textarea>
					</p> 				
					<p>
						<label for="contents" class="describle">我来回答</label><br/>
						<textarea id="contents" name="contents"  rows="9" cols="32" wrap=PHYSICAL
								placeholder="在这里继续补充问题细节"
								oninput="checkinput(this.form.contents,this.form.total2,this.form.used2,this.form.remain2);" 
								onpropertychange=="checkinput(this.form.contents,this.form.total2,this.form.used2,this.form.remain2);"
									></textarea>
					</p>				
					<p>
						<span class="count">
							<input disabled maxLength="4" name="used2" size="3" value="0" 
								class="inputused" style="display:none;"/>
							<input disabled maxLength="4" name="total2" size="3" value="9999" 
								class="inputtotal" style="display:none;"/>				 
							您还可以输入<input disabled maxLength="4" name="remain2" size="3" value="9999" class="inputremain"/>字
						</span>
						<span><input type="submit" value="提交" class="submit"/></span>
					</p><p class="clear"></p>
				</form>
			</div>
		</div>
		<div id="footer">
			<p>©2014 C语言在线答疑系统 v 1.0</p>	
		</div>			
	</div>
</body>
</html>